package com.app.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.app.mapper.PersonPositionMapper;
import com.app.mapper.PersonTrainMapper;
import com.app.model.*;
import com.app.service.PersonPositionService;
import com.app.service.PersonTrainService;
import com.app.util.CommonHelp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
@Primary
public class PersonTrainServiceImpl implements PersonTrainService {
    @Autowired
    PersonTrainMapper personTrainMapper;


    @Override
    public PersonTrainModel selectTrainByPid(int personId) {//查主职
//        QueryWrapper<PersonPositionModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("id","department_id","position","is_major").eq("personId",personId).eq("is_major",1);
//        PersonPositionModel model =  personPositionMapper.selectOne(queryWrapper);
//        return model;
        return null;
    }

    @Override
    public PersonTrainModel selectTrainById(int id) {//根据id单查这一条
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        PersonTrainModel model = personTrainMapper.selectOne(queryWrapper);
        return model;
    }

    @Override
    public List<PersonTrainModel> getTrainList(Integer personId, Integer year) {
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "year","train_result","train_desc", "person_id").eq("person_id", personId);
        if (year != null && year != 0) {
            queryWrapper.eq("year", year);
        }
        List<PersonTrainModel> trainModelList1 = personTrainMapper.selectList(queryWrapper);
        return trainModelList1;
    }

    @Override
    public List<PersonTrainModel> listExport() {
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "train_object", "train_standard", "train_perform", "train_level", "person_id");
        List<PersonTrainModel> trainModelList = personTrainMapper.selectList(queryWrapper);
        return trainModelList;
    }

    @Override
    public Page<PersonTrainModel> getPageTrainList(Integer page, Integer limit) {
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();

        queryWrapper.orderByDesc("create_time");
        Page<PersonTrainModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<PersonTrainModel> trainList = personTrainMapper.selectPage(pages, queryWrapper);
        return trainList;
    }

    @Override
    public void update(PersonTrainModel model) {
        personTrainMapper.updateById(model);
    }

    @Override
    public void insert(PersonTrainModel model) {
        personTrainMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        personTrainMapper.deleteById(id);
    }

    @Override
    public List<PersonTrainModel> getTrainByPerson(List<Integer> idsList) {
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        if (idsList == null || idsList.size() <= 0) {
            queryWrapper.select();
        } else {
            queryWrapper.in("person_id", idsList);
        }
        List<PersonTrainModel> trainModelList = personTrainMapper.selectList(queryWrapper);
        return trainModelList;
    }

    @Override
    public List<PersonTrainModel> trainList(int trainObject) {
        QueryWrapper<PersonTrainModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("train_object", trainObject);
        List<PersonTrainModel> trainModelList = personTrainMapper.selectList(queryWrapper);
        return trainModelList;
    }

    @Override
    public String importPersonTrain(MultipartFile file) throws IOException {
        File localFile = new File("C:\\excel\\");
        file.transferTo(localFile);
        String filePath = localFile.getAbsolutePath();

        EasyExcel.read(filePath, PersonTrainModel.class,new PageReadListener<PersonTrainModel>(dateList->{
            for (PersonTrainModel personTrainModel : dateList) {
                personTrainMapper.insert(personTrainModel);
            }
        })).sheet().headRowNumber(4).doRead();
        localFile.delete();
        return "操作成功";
    }
}
